/*
 * IdleGameStateController.cpp
 *
 *  Created on: Dec 27, 2011
 *      Author: rudi
 */
#include <wx/socket.h>
#include "IdleGameStateController.h"
#include "ServerRegistrationHandler.h"
#include "MainFrame.h"
#include "Configuration.h"
#include "App.h"

IdleGameStateController::IdleGameStateController(MainPanel* mainPanel): mLogoDrawController(mainPanel, true/*reset the cache*/)
{
	wxSocketBase* socket = wxGetApp().getClientSocket();
	wxSocketBase* serverSocket = wxGetApp().getServerSocket();

	// Stop both the server- and the clientsocket if needed
	if(NULL != socket)
	{
		socket->Destroy();
		wxGetApp().setClientSocket(NULL);
	}
	if(NULL != serverSocket)
	{
		// When destroying the server, we need to deregister it first
		ServerRegistrationHandler::ServerInfo serverInfo;
		wxString response;

		serverInfo.setPort(wxGetApp().getConfiguration()->getNetworkConfiguration()->getPortNumber());
		if(false == ServerRegistrationHandler::deregisterServer(serverInfo, response))
			wxGetApp().getMainFrame()->showMessageDialog(wxT("Could not update the server's state: " + response), MainFrame::SERVER_ERROR);
		serverSocket->Destroy();
		wxGetApp().setServerSocket(NULL);
	}
	wxGetApp().resetActualLevel();
	if(NULL == mainPanel)
	{
		// Enable or disable menu points according to the idle state
		wxGetApp().getMainFrame()->setHostEnabled(true);
		wxGetApp().getMainFrame()->setJoinEnabled(true);
		wxGetApp().getMainFrame()->setRestartEnabled(false);
		wxGetApp().getMainFrame()->setMessagingEnabled(false);
		wxGetApp().getMainFrame()->setConfigureEnabled(true);
		wxGetApp().getMainFrame()->GetStatusBar()->SetStatusText(wxT("Idle"));
		wxGetApp().getMainFrame()->setStartHostingEnabled(true);
		wxGetApp().getMainFrame()->setChangeLevelEnabled(false);
		wxGetApp().getMainFrame()->setKickOffClientEnabled(false);
	}
}

/*virtual*/
IdleGameStateController::~IdleGameStateController()
{
	// Nothing to do yet
}

//
// Methods inherited from Controller
//

void IdleGameStateController::handleMouseDown(const wxPoint&)
{
	// Nothing to do
}

void IdleGameStateController::handleMouseUp(const wxPoint&)
{
	// Nothing to do
}

bool IdleGameStateController::handleMouseMotion(const wxPoint&)
{
	// Nothing to do
	return false;
}

const wxBitmap& IdleGameStateController::getBitmapToPaint() const
{
	return mLogoDrawController.getBitmapToPaint();
}

wxBitmap& IdleGameStateController::getBitmapToDraw()
{
	return mLogoDrawController.getBitmapToDraw();
}

//
// Methods inherited from GameStateController
//

void IdleGameStateController::handleSocketConnection(wxSocketEvent&)
{
	wxASSERT_MSG(false, wxT("IdleGameStateController::handleSocketConnection() invoked. It could not happen!"));
}

void IdleGameStateController::handleSocketLost(wxSocketEvent&)
{
	wxASSERT_MSG(false, wxT("IdleGameStateController::handleSocketLost() invoked. It could not happen!"));
}

void IdleGameStateController::handleSocketInput(wxSocketEvent&)
{
	wxASSERT_MSG(false, wxT("IdleGameStateController::handleSocketInput() invoked. It could not happen!"));
}

void IdleGameStateController::handleSocketOutput(wxSocketEvent& socketEvent)
{
	wxASSERT_MSG(false, wxT("IdleGameStateController::handleSocketOutput() invoked. It could not happen!"));
}
